package com.itheima.health.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.constant.MessageConstant;
import com.itheima.health.dao.MemberDao;
import com.itheima.health.dao.OrderDao;
import com.itheima.health.dao.OrderSettingDao;
import com.itheima.health.dao.RoleDao;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.*;
import com.itheima.health.service.OrderService;
import com.itheima.health.service.RoleService;
import com.itheima.health.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @ClassName CheckItemServiceImpl
 * @Description TODO
 * @Author ly
 * @Company 深圳黑马程序员
 * @Date 2019/8/20 16:00
 * @Version V1.0
 */
// dubbo（整合声明式事务处理1：配置applicationContext-tx.xml对类代理，2：@Service(interfaceClass = CheckItemService.class)）
@Service(interfaceClass = RoleService.class)
@Transactional
public class RoleServiceImpl implements RoleService {

    @Autowired
    RoleDao roleDao;


    @Override
    public List<Role> findAll() {
        return roleDao.findAll();

    }

    @Override
    public void add(Role role, Integer[] userIds, Integer[] menuIds) {
        roleDao.add(role);
        setRoleUser(role.getId(),userIds);
        setRoleMenu(role.getId(),menuIds);
    }

    @Override
    public PageResult findPage(String queryString, Integer currentPage, Integer pageSize) {
        // PageHelper的分页助手
        // 1：初始化
        PageHelper.startPage(currentPage,pageSize);
        // 2：执行查询
        Page<Role> page = roleDao.findPage(queryString);
        // 3：组织结果集
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public Role findById(Integer id) {
        return roleDao.findById(id);
    }

    @Override
    public List<Integer> findUserIdsByRoleId(Integer id) {
        return roleDao.findUserIdsByRoleId(id);
    }

    @Override
    public List<Integer> findMenuIdsByRoleId(Integer id) {
        return roleDao.findMenuIdsByRoleId(id);
    }

    @Override
    public void editRole(Role role, Integer[] userIds, Integer[] menuIds) {
        roleDao.editRole(role);
        roleDao.deleteUserIds(role.getId());
        roleDao.deleteMenuIds(role.getId());
        setRoleUser(role.getId(),userIds);
        setRoleMenu(role.getId(),menuIds);
    }

    @Override
    public void deleteRole(Integer id) {
        roleDao.deleteRole_User(id);
        roleDao.deleteRole_Muen(id);
        roleDao.deleteRole(id);

    }

    private void setRoleMenu(Integer roleId, Integer[] menuIds) {
        if(menuIds!=null && menuIds.length>0){
            for (Integer menuId : menuIds) {
                roleDao.setRoleIdAndMenuId(roleId,menuId);
            }
        }
    }

    private void setRoleUser(Integer roleId, Integer[] userIds) {
        if(userIds!=null && userIds.length>0){
            for (Integer userId : userIds) {
                roleDao.setRoleIdAndUserId(roleId,userId);
            }
        }
    }
}
